package com.cg.controller;

import com.cg.dto.BasePageDto;
import com.cg.dto.Result;
import com.cg.dto.ResultEnum;
import com.cg.dto.ResultUtils;
import com.cg.entity.BaseInfo;
import com.cg.service.BaseInfoService;
import com.cg.vo.BaseInfoVo;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * (BaseInfo)表控制层
 *
 * @author chengong
 * @since 2023-12-20 11:08:23
 */
@RestController
@RequestMapping("/baseInfo")
public class BaseInfoController {
    /**
     * 服务对象
     */
    @Resource
    private BaseInfoService baseInfoService;

    /**
     * 验证基础信息表code是否重复
     */
    @GetMapping("/checkBaseCode")
    public Result checkBaseCode(@RequestParam("baseCode") String baseCode){
        Integer count = baseInfoService.checkBaseCode(baseCode);
        if(count > 0){
            return ResultUtils.returnResult(ResultEnum.ERROR_CHECK_BASE_CODE);
        }
        return ResultUtils.returnSuccess();
    }


    /**
     * 基础信息展示
     */
    @PostMapping("/baseList")
    public Result<Map<String,Object>> baseList(@RequestBody BasePageDto basePageDto){
        PageInfo<BaseInfoVo> pageInfo = baseInfoService.baseList(basePageDto);
        Map<String,Object> map = new HashMap<>();
        map.put("pageInfo",pageInfo);
        return ResultUtils.returnDataSuccess(map);
    }


    /**
     * 创建基础信息
     */
    @PostMapping("/insertBaseInfo")
    public Result insertBaseInfo(@RequestBody BaseInfo baseInfo){
        Integer count = baseInfoService.insertBaseInfo(baseInfo);
        if(count <= 0){
            return ResultUtils.returnResult(ResultEnum.ERROR_CHECK_BASE_CODE);
        }
        return ResultUtils.returnSuccess();
    }


    /**
     * 修改基础信息
     */
    @PostMapping("/setBaseInfo")
    public Result setBaseInfo(@RequestBody BaseInfo baseInfo){
        Integer count = baseInfoService.setBaseInfo(baseInfo);
        if(count <= 0){
            return ResultUtils.returnFail("修改失败");
        }
        return ResultUtils.returnSuccess();
    }


    /**
     * 删除基础信息
     */
    @PostMapping("/removeBaseInfo")
    public Result removeBaseInfo(@RequestParam("baseInfoId") Integer baseInfoId){
        Integer count = baseInfoService.removeBaseInfo(baseInfoId);
        if(count <= 0){
            return ResultUtils.returnFail("删除失败");
        }
        return ResultUtils.returnSuccess();
    }

}

